package mark.tom.pistols.decryptor;

import mark.tom.pistols.algorithm.encryption.Base64Encryption;
import mark.tom.pistols.algorithm.encryption.RSAEncryption;

public final class DataDecrypt {

    private final static int RSA_DECRYPT_DATA_LENGTH = 172;

    public static String getDecryptData(String input, String privateKey) throws Exception {
        StringBuilder sb = new StringBuilder();
        int times = input.length() / RSA_DECRYPT_DATA_LENGTH;
        if (input.length() % RSA_DECRYPT_DATA_LENGTH != 0) {
            times++;
        }
        for (int i = 0; i < times; i++) {
            int startIndex = RSA_DECRYPT_DATA_LENGTH * i;
            int endIndex = RSA_DECRYPT_DATA_LENGTH * (i + 1);
            String subStr = input.substring(startIndex, endIndex);
            sb.append(getDecryptStr(subStr, privateKey));
        }
        return sb.toString();
    }

    private static String getDecryptStr(String input, String privateKey) throws Exception {
        return new String(RSAEncryption.decrypt(privateKey, Base64Encryption.decrypt(input)), "utf-8");
    }
}
